﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using Yeti_v0._01.App_code;

namespace Yeti_v0._01
{
    [Serializable()]
    public class SessionLog
    {
       

        public Session session { get; private set; }
        // yetiEntities yE = new yetiEntities();
        private List<Turn> turns = new List<Turn>();   
        public Pattern CurrentPattern { get; private set; }
        public Response CurrentResponse { get; private set; }
        


        public SessionLog()
        {
            session = new Session();
        }

        public SessionLog(string userIp, string userName, DateTime startDate)
        {
            session = new Session();
            //getting user name, user_ip and start time
            session.Start_date = startDate;
            session.User_IP = userIp;
            session.User_name = userName;

        }

        public void FillSessionLogWithAllData(List<SerializableTurn> serializableTurns, yetiEntities yE)
        {
            //preparing each turn, pattern,response and topic
            foreach (var sT in serializableTurns)
            {
                //serializableTurn.

                var topic = new Topic();
                topic = SharedDbFunctions.GetTopicByPatternContent(sT.patternContent);


                var pattern = new Pattern();
                pattern = ConvertToPatternEntity(sT.patternContent, sT.userInput, topic);

                var response = new Response();
                response = ConvertToResponseEntity(sT.responseContent, pattern);

                var turn = new Turn();
                turn = ConvertToTurnEntity(sT.dateStamp, session, pattern, response);
                
                turns.Add(turn);

            }

            //filling current session with all turns
            FillSessionWithTurns();
            
            //saving changes to db
            SaveSessionLogToDb(yE);

        }


        public void tescik()
        {
            Response resp = new Response();
            Pattern patt = new Pattern();
            Topic top = new Topic();
            Subject subj = new Subject();
            subj.Name = "subj";         

            top.Name = "siema";
            top.Subject = subj;

            patt.Content = "Siemanko";
            patt.User_input = "jhjh";
            patt.Topic = top;
            resp.Accuracy = 50;
            resp.Certainty = 50;
            resp.Content = "hejhajhello";
            resp.Pattern = patt;

            yetiEntities yE = new yetiEntities();
            yE.Patterns.AddObject(patt);
            yE.SaveChanges();
        }

        private void FillSessionWithTurns()
        {
            foreach (var turn in turns)
            {
                session.Turns.Add(turn);
            }
            //session.Turns.Add

        }

        public Pattern ConvertToPatternEntity(string patternContent ,string userInput, Topic topic)
        {
            var pattern = new Pattern {Content = patternContent, Topic = topic, User_input = userInput};

            return pattern;

            // CurrentPattern = new Pattern();
            // CurrentPattern = pattern;

        }

        public Response ConvertToResponseEntity( string responseContent, Pattern pattern /*in plans: int Accuracy, int Certainty*/ )
        {
            var response = new Response
                               {Pattern = pattern, Content = responseContent, Accuracy = 50, Certainty = 50};

            return response;
            //CurrentResponse = new Response();
            //CurrentResponse = response;

        }

        public Turn ConvertToTurnEntity(DateTime dateStamp, Session session, Pattern pattern, Response response)
        {
            var turn = new Turn
                           {
                               Session = session,
                               Pattern = pattern,
                               Response = response,
                               Datestamp = dateStamp
                           };
            return turn;

            

        }


        public void SaveSessionLogToDb(yetiEntities yE)
        {
           
            session.End_date = DateTime.Now;

            yE.Sessions.AddObject(session);
           // yE.Topics.Detach();

            //adding topics is unnecessary
            foreach (var turn in turns)
            {
                yE.Topics.Detach(turn.Pattern.Topic);
            }

            //duplicated patterns in response
            //foreach (var turn in turns)
            //{
            //    yE.Patterns.Detach(turn.Response.Pattern);
            //}
            yE.SaveChanges();
        }










    }
}